﻿@using Smartstore.Web.Models.Catalog
@using Smartstore.Core.Catalog.Products

@model ProductDetailsModel

@{
    var conditionContent = "http://schema.org/";

    switch (Model.Condition)
    {
        case ProductCondition.Damaged:
            conditionContent += "DamagedCondition";
            break;
        case ProductCondition.Used:
            conditionContent += "UsedCondition";
            break;
        case ProductCondition.Refurbished:
            conditionContent += "RefurbishedCondition";
            break;
        case ProductCondition.New:
        default:
            conditionContent += "NewCondition";
            break;
    }
}

<div class="pd-offer" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
    <meta itemprop="itemCondition" content="@conditionContent" />
    <link itemprop="url" href="@Url.RouteUrl("Product", new { SeName = Model.SeName }, Context.Request.Scheme)" />
    @if (!Model.Price.HidePrices || Model.Price.ShowLoginNote)
    {
        if (Model.AddToCart.CustomerEntersPrice)
        {
            var dataDictCustomerPrice = new ViewDataDictionary(ViewData);
            dataDictCustomerPrice.TemplateInfo.HtmlFieldPrefix = string.Format("addtocart_{0}", Model.Id);
            <partial name="Product.CustomerPrice" model="Model" view-data="dataDictCustomerPrice" />
        }
        else
        {
            <div class="pd-offer-price-container" data-partial="Price">
                <partial name="Product.Offer.Price" model="Model" />
            </div>
        }
    }

    <div class="pd-stock-info-container" data-partial="Stock">
        <partial name="Product.StockInfo" model="Model" />
    </div>

    @if (Model.GiftCard.IsGiftCard)
    {
        var dataDictGiftCard = new ViewDataDictionary(ViewData);
        dataDictGiftCard.TemplateInfo.HtmlFieldPrefix = Model.GiftCardFieldPrefix;
        <partial name="Product.GiftCard" model="Model.GiftCard" view-data="dataDictGiftCard" />
    }

    @if (Model.ProductType != ProductType.BundledProduct && Model.ProductVariantAttributes.Any(x => x.ShouldBeRendered))
    {
        <div class="pd-variants-container" data-partial="Variants" role="group" aria-label="@T("Aria.Label.ProductVariants")">
            @{
                var dataDictVariants = new ViewDataDictionary(ViewData);
                dataDictVariants.TemplateInfo.HtmlFieldPrefix = string.Format("attributes_{0}", Model.Id);
                <partial name="Product.Variants" model="Model.ProductVariantAttributes" view-data="dataDictVariants" />
            }
        </div>
    }

    <div class="pd-offer-actions-container" data-partial="OfferActions">
        @{
            var dataDictAddToCart = new ViewDataDictionary(ViewData);
            dataDictAddToCart.TemplateInfo.HtmlFieldPrefix = string.Format("addtocart_{0}", Model.Id);
            <partial name="Product.Offer.Actions" model="Model" view-data="dataDictAddToCart" />
        }
    </div>

    <partial name="Product.Offer.Actions.Misc" model="Model" />
</div>

<zone name="productdetail_offer_after" />

<script sm-target-zone="scripts" sm-minify="true" data-origin="product-detail-offer">
    // Ask question
    $(document).on('click', '.action-ask-question', function (e) {
        e.preventDefault();
        const el = $(this);
		const form = el.data('form-selector');
		if (form) {
			const href = el.data('href');
            $.ajax({
				cache: false,
                url: href,
                data: $(form).serialize(),
                success: function (response) {
					// Redirect to ask question get action
					if (response.redirect)
						location.href = response.redirect;
				}
            });
        }
    });
</script>